/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include ".\mmachineinfo.h"

//---------------------------------------------------------------------------

MMachineInfo::MMachineInfo(void)
{
	Initialize();
}

MMachineInfo::~MMachineInfo(void)
{
	CleanUp();
}

//---------------------------------------------------------------------------

void MMachineInfo::CleanUp(void)
{
	thePortList.Clear();
	theProtoList.Clear();
	theIgmpPackList.Clear();
	theGxRPList.Clear();
	theStpList.Clear();
	theCdpList.Clear();
	theDtpList.Clear();
	theDnsNameList.Clear();
	theShareInfoList.Clear();

	snmpSysRec.Initialize();

	infoDHCP.Initialize();

	wmiData.Initialize();
}

//---------------------------------------------------------------------------

void MMachineInfo::Initialize(void)
{
	int i;

	thePortList.Clear();
	theProtoList.Clear();
	theIgmpPackList.Clear();
	theGxRPList.Clear();
	theStpList.Clear();
	theCdpList.Clear();
	theDtpList.Clear();
	theDnsNameList.Clear();
	theShareInfoList.Clear();

	address.Initialize();

	pingReplayTime = -1;

	recvFin         = false;
	recvNull        = false;
	recvXmas        = false;
	recvUdpSend     = false;
	recvIpProto     = false;
	recv6Fin        = false;
	recv6Null       = false;
	recv6Xmas       = false;
	recv6UdpSend    = false;
	recv6NextHeader = false;

	nbNamesCnt = 0;
	for(i=0; i<MAX_NB_NAMES; i++)
		nbNames[i].Empty();

	snmpSysRec.Initialize();

	infoDHCP.Initialize();

	ssdp = false;

	wmiData.Initialize();

	successfullyStopped = false;

	hunted = false;

	countryCode.Empty();
}

bool MMachineInfo::LoadFromPointer(MMachineInfo* mmiIn)
{
	Initialize();
	return LoadFromPointerUpdate(mmiIn);
}

bool MMachineInfo::LoadFromPointerUpdate(MMachineInfo* mmiIn)
{
	MPortInfo   *mpi;
	MIGMPPacket *mpg;
	MGxRPPacket *mpgx;
	MSTPPacket  *stpx;
	MCDPPacket  *cdpx;
	MDTPPacket  *dtpx;
	MDnsName    *dnsName;
	MShareInfo  *shareInfo;
	int i, cnt;
	bool err;

	if(mmiIn==NULL) return true;

	err = false;

	// fill the list of ports
	cnt = mmiIn->GetPortCount();
	for(i=0; i<cnt && !err; i++){
		mpi = mmiIn->GetPortInfo(i);
		if(!thePortList.AddNoDuplicates(mpi))
			err = true;
	}
	if(err){
		Initialize();
		return false;
	}

	// fill the list of protocols
	cnt = mmiIn->GetProtoCount();
	for(i=0; i<cnt && !err; i++){
		mpi = mmiIn->GetProtoInfo(i);
		if(!theProtoList.AddNoDuplicates(mpi))
			err = true;
	}
	if(err){
		Initialize();
		return false;
	}

	// fill the list of IGMP packets
	cnt = mmiIn->GetIgmpPackCount();
	for(i=0; i<cnt && !err; i++){
		mpg = mmiIn->GetIgmpPack(i);
		if(!theIgmpPackList.AddNoDuplicates(mpg))
			err = true;
	}
	if(err){
		Initialize();
		return false;
	}

	// fill the list of GxRP packets
	cnt = mmiIn->GetGxRPPackCount();
	for(i=0; i<cnt && !err; i++){
		mpgx = mmiIn->GetGxRPPack(i);
		if(!theGxRPList.AddNoDuplicates(mpgx))
			err = true;
	}
	if(err){
		Initialize();
		return false;
	}

	// fill the list of STP packets
	cnt = mmiIn->GetSTPPackCount();
	for(i=0; i<cnt && !err; i++){
		stpx = mmiIn->GetSTPPack(i);
		if(!theStpList.AddNoDuplicates(stpx))
			err = true;
	}
	if(err){
		Initialize();
		return false;
	}

	// fill the list of CDP packets
	cnt = mmiIn->GetCDPPackCount();
	for(i=0; i<cnt && !err; i++){
		cdpx = mmiIn->GetCDPPack(i);
		if(!theCdpList.AddNoDuplicates(cdpx))
			err = true;
	}
	if(err){
		Initialize();
		return false;
	}

	// fill the list of DTP packets
	cnt = mmiIn->GetDTPPackCount();
	for(i=0; i<cnt && !err; i++){
		dtpx = mmiIn->GetDTPPack(i);
		if(!theDtpList.AddNoDuplicates(dtpx))
			err = true;
	}
	if(err){
		Initialize();
		return false;
	}

	// fill the address
	address.LoadFromPointer(&mmiIn->address);

	// fill the list of DNS names
	cnt = mmiIn->GetDnsNameCount();
	for(i=0; i<cnt && !err; i++){
		dnsName = mmiIn->GetDnsName(i);
		if(!theDnsNameList.AddNoDuplicates(dnsName))
			err = true;
	}
	if(err){
		Initialize();
		return false;
	}

	// fill the list of share info
	cnt = mmiIn->GetShareInfoCount();
	for(i=0; i<cnt && !err; i++){
		shareInfo = mmiIn->GetShareInfo(i);
		if(!theShareInfoList.AddNoDuplicates(shareInfo))
			err = true;
	}
	if(err){
		Initialize();
		return false;
	}

	// fill ping related values
	if(mmiIn->pingReplayTime>0)
		pingReplayTime  = mmiIn->pingReplayTime;

	// fill received packets status
	recvFin         = mmiIn->recvFin;
	recvNull        = mmiIn->recvNull;
	recvXmas        = mmiIn->recvXmas;
	recvUdpSend     = mmiIn->recvUdpSend;
	recvIpProto     = mmiIn->recvIpProto;
	recv6Fin        = mmiIn->recv6Fin;
	recv6Null       = mmiIn->recv6Null;
	recv6Xmas       = mmiIn->recv6Xmas;
	recv6UdpSend    = mmiIn->recv6UdpSend;
	recv6NextHeader = mmiIn->recv6NextHeader;

	// fill NetBIOS names
	nbNamesCnt = mmiIn->nbNamesCnt;
	for(i=0; i<nbNamesCnt; i++)
		nbNames[i] = mmiIn->nbNames[i];

	// fill SNMP informations
	snmpSysRec.LoadFromPointer(&mmiIn->snmpSysRec);

	// fill DHCP informations
	infoDHCP.LoadFromPointer(&mmiIn->infoDHCP);

	// fill SSDP status
	if(mmiIn->ssdp)
		ssdp = true;

	// fill WMI data
	wmiData.LoadFromPointer(&mmiIn->wmiData);

	// fill successfullyStopped
	if(mmiIn->successfullyStopped)
		successfullyStopped = true;

	// fill hunted
	if(mmiIn->hunted)
		hunted = true;

	// fill country code
	if(countryCode.IsEmpty())
		countryCode = mmiIn->countryCode;

	return true;
}

//---------------------------------------------------------------------------

bool MMachineInfo::AddPortInfo(MPortInfo* packIn)
{
	return thePortList.Add(packIn);
}

bool MMachineInfo::AddPortInfoNoDuplicates(MPortInfo* packIn)
{
	return thePortList.AddNoDuplicates(packIn);
}

int MMachineInfo::GetPortCount(void)
{
	return thePortList.GetSize();
}

MPortInfo* MMachineInfo::GetPortInfo(int idxIn)
{
	return thePortList.GetByIdx(idxIn);
}

//---------------------------------------------------------------------------

bool MMachineInfo::AddProtoInfo(MPortInfo* packIn)
{
	return theProtoList.Add(packIn);
}

int MMachineInfo::GetProtoCount(void)
{
	return theProtoList.GetSize();
}

MPortInfo* MMachineInfo::GetProtoInfo(int idxIn)
{
	return theProtoList.GetByIdx(idxIn);
}

//---------------------------------------------------------------------------

bool MMachineInfo::AddIgmpPack(MIGMPPacket* packIn)
{
	return theIgmpPackList.Add(packIn);
}

bool MMachineInfo::AddIgmpPackNoDuplicates(MIGMPPacket* packIn)
{
	return theIgmpPackList.AddNoDuplicates(packIn);
}

int MMachineInfo::GetIgmpPackCount(void)
{
	return theIgmpPackList.GetSize();
}

MIGMPPacket* MMachineInfo::GetIgmpPack(int idxIn)
{
	return theIgmpPackList.GetByIdx(idxIn);
}

//---------------------------------------------------------------------------

bool MMachineInfo::AddGxRPPack(MGxRPPacket* packIn)
{
	return theGxRPList.Add(packIn);
}

bool MMachineInfo::AddGxRPPackNoDuplicates(MGxRPPacket* packIn)
{
	return theGxRPList.AddNoDuplicates(packIn);
}

int MMachineInfo::GetGxRPPackCount(void)
{
	return theGxRPList.GetSize();
}

MGxRPPacket* MMachineInfo::GetGxRPPack(int idxIn)
{
	return theGxRPList.GetByIdx(idxIn);
}

//---------------------------------------------------------------------------

bool MMachineInfo::AddSTPPack(MSTPPacket* packIn)
{
	return theStpList.Add(packIn);
}

bool MMachineInfo::AddSTPPackNoDuplicates(MSTPPacket* packIn)
{
	return theStpList.AddNoDuplicates(packIn);
}

int MMachineInfo::GetSTPPackCount(void)
{
	return theStpList.GetSize();
}

MSTPPacket* MMachineInfo::GetSTPPack(int idxIn)
{
	return theStpList.GetByIdx(idxIn);
}

//---------------------------------------------------------------------------

bool MMachineInfo::AddCDPPack(MCDPPacket* packIn)
{
	return theCdpList.Add(packIn);
}

bool MMachineInfo::AddCDPPackNoDuplicates(MCDPPacket* packIn)
{
	return theCdpList.AddNoDuplicates(packIn);
}

int MMachineInfo::GetCDPPackCount(void)
{
	return theCdpList.GetSize();
}

MCDPPacket* MMachineInfo::GetCDPPack(int idxIn)
{
	return theCdpList.GetByIdx(idxIn);
}

//---------------------------------------------------------------------------

bool MMachineInfo::AddDTPPack(MDTPPacket* packIn)
{
	return theDtpList.Add(packIn);
}

bool MMachineInfo::AddDTPPackNoDuplicates(MDTPPacket* packIn)
{
	return theDtpList.AddNoDuplicates(packIn);
}

int MMachineInfo::GetDTPPackCount(void)
{
	return theDtpList.GetSize();
}

MDTPPacket* MMachineInfo::GetDTPPack(int idxIn)
{
	return theDtpList.GetByIdx(idxIn);
}

//---------------------------------------------------------------------------

bool MMachineInfo::AddDnsName(MDnsName* dnsNameIn)
{
	return theDnsNameList.Add(dnsNameIn);
}

bool MMachineInfo::AddDnsNameNoDuplicates(MDnsName* dnsNameIn)
{
	return theDnsNameList.AddNoDuplicates(dnsNameIn);
}

int MMachineInfo::GetDnsNameCount(void)
{
	return theDnsNameList.GetSize();
}

MDnsName* MMachineInfo::GetDnsName(int idxIn)
{
	return theDnsNameList.GetByIdx(idxIn);
}

//---------------------------------------------------------------------------

bool MMachineInfo::AddShareInfo(MShareInfo* shareInfoIn)
{
	return theShareInfoList.Add(shareInfoIn);
}

bool MMachineInfo::AddShareInfoNoDuplicates(MShareInfo* shareInfoIn)
{
	return theShareInfoList.AddNoDuplicates(shareInfoIn);
}

int MMachineInfo::GetShareInfoCount(void)
{
	return theShareInfoList.GetSize();
}

MShareInfo* MMachineInfo::GetShareInfo(int idxIn)
{
	return theShareInfoList.GetByIdx(idxIn);
}

//---------------------------------------------------------------------------

void MMachineInfo::SortPortInfo(void)
{
	thePortList.SortTheList();
}

void MMachineInfo::SortProtoInfo(void)
{
	theProtoList.SortTheList();
}

void MMachineInfo::SortIgmpPacks(void)
{
	theIgmpPackList.SortTheList();
}

void MMachineInfo::SortShareInfo(void)
{
	theShareInfoList.SortTheList();
}

//---------------------------------------------------------------------------

bool MMachineInfo::IsTcpPortOpen(WORD portIn)
{
	MPortInfo* portInfo;
	int i, cnt;
	bool res;

	res = false;
	cnt = thePortList.GetSize();
	for(i=0; i<cnt && !res; i++){
		portInfo = thePortList.GetByIdx(i);
		if(portInfo!=NULL){
			if(portInfo->port==portIn)
				if(portInfo->IsTcpPort())
					res = true;
		}
	}

	return res;
}

bool MMachineInfo::IsUdpPortOpen(WORD portIn)
{
	MPortInfo* portInfo;
	int i, cnt;
	bool res;

	res = false;
	cnt = thePortList.GetSize();
	for(i=0; i<cnt && !res; i++){
		portInfo = thePortList.GetByIdx(i);
		if(portInfo!=NULL){
			if(portInfo->port==portIn)
				if(portInfo->IsUdpPort())
					res = true;
		}
	}

	return res;
}

//---------------------------------------------------------------------------
